package com.qf.shiro2203.userr.controller;

import com.qf.shiro2203.userr.entity.User;
import com.qf.shiro2203.userr.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wawawa
 * @since 2023-02-09
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    @PostMapping("/login")
    public String login(String username,String password,boolean rememberMe){
        //基于shiro的登录方法
        //Subject代表当前用户信息的对象
        Subject subject = SecurityUtils.getSubject();
        //设置免登录信息
        //将用户提供的用户名和密码封装成 shiro中需要的  UsernamePasswordToken对象
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password,rememberMe);
        //让shiro处理登录的身份验证逻辑
        subject.login(usernamePasswordToken);
        /*现在需要拿到数据库中的用户名和密码   在CustomRealm中*/

        //如果没问题，返回登录成功
        return "redirect:/home.html";
    }
    @PostMapping("/register")
    public String regist(User user){
        userService.regis(user);

        //注册成功重定向到登录页面
        return "redirect:/login.html";
    }

}
